package org.example.thread;

import java.util.concurrent.*;

public class CompleTableFutureDemo05 {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        ExecutorService executor =    new ThreadPoolExecutor(4, 8,
                1000L, TimeUnit.MILLISECONDS,
                new LinkedBlockingQueue<Runnable>(10),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.AbortPolicy());


        CompletableFuture<String> completableFuture1 = CompletableFuture.supplyAsync(()->{
            try {
                TimeUnit.SECONDS.sleep(2);
            } catch (InterruptedException e) {
            }
            System.out.println("执行1 ...");
            return "订航班";
        },executor);
        System.out.println(completableFuture1.get());

        CompletableFuture<String> completableFuture2 = CompletableFuture.supplyAsync(()->{
            try {
                TimeUnit.SECONDS.sleep(2);
            } catch (InterruptedException e) {
            }
            System.out.println("执行2 ...");
            return "订酒店";
        },executor);
        System.out.println(completableFuture2.join());
        CompletableFuture<String> completableFuture3 = completableFuture2.thenCombineAsync(completableFuture1,(hotel,airplane) -> {
            System.out.println(airplane+hotel+"之后执行");
            try {
                Thread.sleep(1000L);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return "租车";
        });

        System.out.println(completableFuture3.join());

        executor.shutdown();
    }

}
